package com.yax.util;



import com.alibaba.fastjson.JSONObject;
import javafx.application.Platform;
import javafx.scene.control.Alert;
import javafx.scene.control.ProgressBar;
import javafx.scene.control.ProgressIndicator;
import javafx.stage.Stage;


import java.io.File;
import java.io.IOException;
import java.util.HashMap;


import java.util.Map;
import java.util.logging.Logger;


/**
 * @Author: yax
 * @Date: 2021/1/28 16:06
 */
public class RockUtil {
    private static Logger log = Logger.getLogger("OkHttpUtil");
    private static String exepath=System.getProperty("exe.path");
    private static String exeAddress=System.getProperty("exe4j.moduleName");

    public static String getExepath() {
        return exepath;
    }

    public static  Map<String,String> loginRock(String url,String account,String password){
        HashMap<String, String > param=new HashMap<>();
        param.put("account",account);
        param.put("password",password);
        Map<String,String> result=OkHttpUtil.postForCookie(url+"/zl.fp.user.login/1.0.0/action",param);
        return result;
    }

    public static JSONObject getRiskDetailByKeyWord(String url,String cookie,String keyword){
        HashMap<String, String > param=new HashMap<>();
        param.put("keyword",keyword);
        String res=OkHttpUtil.post(url+"/zl.fp.service.query/1.0.0/action",cookie,param);
        return JSONObject.parseObject(res);
        //return (Map<String, String>) JSONObject.parseObject(res).getJSONObject("data").getJSONArray("list").get(0);
    }

    public static JSONObject getRiskList(String url,String cookie,Map<String,String> param){
        String res=OkHttpUtil.post(url+"/zl.fp.service.query/1.0.0/action",cookie,param);
        return JSONObject.parseObject(res);
    }

    public static JSONObject synRisk(String env,String cookie,Map<String, String > param){
        param.remove("id");
        String res=OkHttpUtil.post(env+"/zl.fp.service.add/1.0.0/action",cookie,param);
        return JSONObject.parseObject(res);
    }
    public static JSONObject getAppList(String env,String cookie,Map<String,String> param){
        String res=OkHttpUtil.post(env+"/zl.fp.appauthcode.queryapplist/1.0.0/action",cookie,param);
        return JSONObject.parseObject(res);
    }

    public static JSONObject getVersion(){
        try {
            String res = OkHttpUtil.post("http://120.92.153.28:8080/gitlog/rock/version", "", null);
            log.info("版本信息 " + res);
            JSONObject jsonObject = JSONObject.parseObject(res);
            return jsonObject.getJSONObject("data");
        }catch (Exception e){
            e.printStackTrace();
            return new JSONObject();
        }
    }
    public static void downloadRock(ProgressBar pb, ProgressIndicator pin, String version, String newVersion, Stage primaryStage){
        String downLoad=System.getProperty("user.dir");
        String fileName="rock-"+newVersion+".exe";
        //String oldFileName="yapi-"+version+".exe";
        String exepath=System.getProperty("exe.path");
        log.info("当前下载路径:"+downLoad+"文件名:"+fileName +"exe路径:"+exepath);
        OkHttpUtil.download("http://120.92.153.28:8080/gitlog/rock/downLoad",downLoad,fileName,new OkHttpUtil.OnDownloadListener(){

            @Override
            public void onDownloadSuccess(File file) {
                Thread thread =new Thread(()->{
                    try {
                        Runtime.getRuntime().exec(downLoad+"\\"+fileName+" "+exeAddress);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    System.exit(0);
                });
                thread.start();
            }

            @Override
            public void onDownloading(int progress) {
                Platform.runLater(() -> {
                    pb.setProgress(progress/100d);
                    pin.setProgress(progress/100d);
                });
            }

            @Override
            public void onDownloadFailed(Exception e) {
                Platform.runLater(() -> {
                    Alert alert = new Alert(Alert.AlertType.ERROR);
                    alert.setTitle("异常");
                    alert.setHeaderText("下载错误");
                    alert.setContentText("下载出错!");
                });
            }
        });
    }
}
